/*
Copyright (c) 2008 Ryan BB Schreiber, Brendan G Fry

Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to 
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do 
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/
using System;
using System.Windows.Forms;
using Darkside.GeneratorStudio.Library.Domain;

namespace Darkside.GeneratorStudio.UI.UserControls
{
    public partial class TableInfoControl : UserControl
    {
		private Table _ItemObject;

        private TableInfoControl()
        {
            InitializeComponent();
        }

        public TableInfoControl(Project project, Table table)
            : this()
        {
            Populate(project, table);
        }

        public Table UpdateItemObject()
		{

			_ItemObject.Settings.Active = chkActive.Checked;
			for (int i = 0; i < lstParentTables.Items.Count; i++)
			{
				_ItemObject.ParentTables[i].IsActive = lstParentTables.GetItemChecked(i);
			}
			for (int j = 0; j < lstChildTables.Items.Count; j++)
			{
                _ItemObject.ChildTables[j].IsActive = lstChildTables.GetItemChecked(j);
			}
			return _ItemObject;
		}

		private void LoadForm()
		{
			this.Tag = _ItemObject;
			chkCheckAll.Checked = false;
            txtIsView.Text = _ItemObject.IsView ? "True" : "False";
            groupBox1.Text = _ItemObject.IsView ? "View Information" : "Table Information";
			if (_ItemObject.Settings.LastGenerateDate == new DateTime(1, 1, 1))
			{
				txtGenDate.Text = "Never";
			}
			else
			{
				txtGenDate.Text = _ItemObject.Settings.LastGenerateDate.ToString("dd MMM yyyy hh:mm:ss");
			}
			txtGenVersion.Text = _ItemObject.Settings.LastGenerateVersion.ToString("0");
			chkActive.Checked = _ItemObject.Settings.Active;

            for (int i = 0; i < _ItemObject.ParentTables.Count; i++)
            {
                lstParentTables.Items.Add(_ItemObject.ParentTables[i], _ItemObject.ParentTables[i].IsActive);
            }

            for (int j = 0; j < _ItemObject.ChildTables.Count; j++)
            {
                lstChildTables.Items.Add(_ItemObject.ChildTables[j], _ItemObject.ChildTables[j].IsActive);
            }
		}
		
		public void Populate(Project project, Table table)
        {
            lblTableName.Text = table.Name;
            lstParentTables.Items.Clear();
            lstChildTables.Items.Clear();
		    _ItemObject = table;
			LoadForm();
        }

		public void Populate(Table popTable)
		{
			lblTableName.Text = popTable.Name;
			lstParentTables.Items.Clear();
			lstChildTables.Items.Clear();
			_ItemObject = popTable;
			LoadForm();
		}

		private void chkCheckAll_CheckedChanged(object sender, EventArgs e)
		{
			if (chkCheckAll.Checked)
			{
				for (int i = 0; i < lstParentTables.Items.Count; i++)
				{
					lstParentTables.SetItemChecked(i, true);
				}

				for (int j = 0; j < lstChildTables.Items.Count; j++)
				{
					lstChildTables.SetItemChecked(j, true);
				}
			}
		}
    }
}
